{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Count Pairs Of Similar Strings"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #hash-table #string"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #哈希表 #字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: similarPairs"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #统计相似字符串对的数目"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个下标从 <strong>0</strong> 开始的字符串数组 <code>words</code> 。</p>\n",
    "\n",
    "<p>如果两个字符串由相同的字符组成，则认为这两个字符串 <strong>相似</strong> 。</p>\n",
    "\n",
    "<ul>\n",
    "\t<li>例如，<code>\"abca\"</code> 和 <code>\"cba\"</code> 相似，因为它们都由字符 <code>'a'</code>、<code>'b'</code>、<code>'c'</code> 组成。</li>\n",
    "\t<li>然而，<code>\"abacba\"</code> 和 <code>\"bcfd\"</code> 不相似，因为它们不是相同字符组成的。</li>\n",
    "</ul>\n",
    "\n",
    "<p>请你找出满足字符串&nbsp;<code>words[i]</code><em> </em>和<em> </em><code>words[j]</code> 相似的下标对<em> </em><code>(i, j)</code><em> </em>，并返回下标对的数目，其中 <code>0 &lt;= i &lt; j &lt;= word.length - 1</code> 。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>words = [\"aba\",\"aabb\",\"abcd\",\"bac\",\"aabc\"]\n",
    "<strong>输出：</strong>2\n",
    "<strong>解释：</strong>共有 2 对满足条件：\n",
    "- i = 0 且 j = 1 ：words[0] 和 words[1] 只由字符 'a' 和 'b' 组成。 \n",
    "- i = 3 且 j = 4 ：words[3] 和 words[4] 只由字符 'a'、'b' 和 'c' 。 \n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>words = [\"aabb\",\"ab\",\"ba\"]\n",
    "<strong>输出：</strong>3\n",
    "<strong>解释：</strong>共有 3 对满足条件：\n",
    "- i = 0 且 j = 1 ：words[0] 和 words[1] 只由字符 'a' 和 'b' 组成。 \n",
    "- i = 0 且 j = 2 ：words[0] 和 words[2] 只由字符 'a' 和 'b' 组成。 \n",
    "- i = 1 且 j = 2 ：words[1] 和 words[2] 只由字符 'a' 和 'b' 组成。 \n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>words = [\"nba\",\"cba\",\"dba\"]\n",
    "<strong>输出：</strong>0\n",
    "<strong>解释：</strong>不存在满足条件的下标对，返回 0 。</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= words.length &lt;= 100</code></li>\n",
    "\t<li><code>1 &lt;= words[i].length &lt;= 100</code></li>\n",
    "\t<li><code>words[i]</code> 仅由小写英文字母组成</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [count-pairs-of-similar-strings](https://leetcode.cn/problems/count-pairs-of-similar-strings/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [count-pairs-of-similar-strings](https://leetcode.cn/problems/count-pairs-of-similar-strings/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[\"aba\",\"aabb\",\"abcd\",\"bac\",\"aabc\"]', '[\"aabb\",\"ab\",\"ba\"]', '[\"nba\",\"cba\",\"dba\"]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def similarPairs(self, words: List[str]) -> int:\n",
    "        c=0\n",
    "        for i in range(len(words)):\n",
    "            for j in range(i+1,len(words)):\n",
    "                if set(words[i])==set(words[j]):\n",
    "                    c+=1\n",
    "        return c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def similarPairs(self, words: list) -> int:\n",
    "        hash_d = {}\n",
    "        res = 0\n",
    "        for word in words:\n",
    "            word_set = \"\".join(sorted(list(set(word))))\n",
    "            res += hash_d.get(word_set,0)\n",
    "            hash_d[word_set] = hash_d.get(word_set,0) + 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def similarPairs(self, words: List[str]) -> int:\n",
    "        import collections\n",
    "        count = len(words)\n",
    "        result = 0\n",
    "        for i in range(count-1):\n",
    "            for j in range(1+i, count):\n",
    "                if set(collections.Counter(words[i])) == set(collections.Counter(words[j])):\n",
    "                    result += 1\n",
    "        return result\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def similarPairs(self, words: List[str]) -> int:\n",
    "        ans, cnt = 0, Counter()\n",
    "        for s in words:\n",
    "            mask = 0\n",
    "            for c in s:\n",
    "                mask |= 1 << (ord(c) - ord('a'))\n",
    "            ans += cnt[mask]\n",
    "            cnt[mask] += 1\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def similarPairs(self, words: List[str]) -> int:\n",
    "        ans, cnt = 0, Counter()\n",
    "        for s in words:\n",
    "            mask = 0\n",
    "            for c in s:\n",
    "                mask |= 1 << (ord(c) - ord('a'))\n",
    "            ans += cnt[mask]\n",
    "            cnt[mask] += 1\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "def do_hash(word):\n",
    "   _id = ['0' for _ in range(26)]\n",
    "   for ch in word:\n",
    "       idx = ord(ch) - ord('a')\n",
    "       _id[idx] = '1'\n",
    "   \n",
    "   return \"\".join(_id)\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    pass\n",
    "    \n",
    "    def similarPairs(self, words: List[str]) -> int:\n",
    "        mp = {}\n",
    "        for word in words:\n",
    "            _id = do_hash(word)\n",
    "            mp[_id] = mp.get(_id, 0) + 1\n",
    "        \n",
    "        res = 0\n",
    "        for val in mp.values():\n",
    "            res += val*(val-1)//2\n",
    "        \n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def similarPairs(self, words: List[str]) -> int:\n",
    "        result=0\n",
    "        for i in range(len(words)):\n",
    "            for j in range(i+1,len(words)):\n",
    "                a1=set(words[i])\n",
    "                a2=set(words[j])\n",
    "                if(a1==a2):\n",
    "                    result+=1\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    # 判断两个字符串包含的字母是否相同\n",
    "    def isSame(self,word1: str, word2: str):\n",
    "        chr_in_word1 = []\n",
    "        chr_in_word2 = []\n",
    "        for i in range(len(word1)):\n",
    "            if word1[i] not in word2:\n",
    "                chr_in_word1.append(word1[i])\n",
    "        chr_in_word1.sort()\n",
    "        for i in range(len(word2)):\n",
    "            if word2[i] not in word1:\n",
    "                chr_in_word2.append(word2[i])\n",
    "        chr_in_word2.sort()\n",
    "        if chr_in_word1 == chr_in_word2:\n",
    "            return True\n",
    "        else:\n",
    "            return False\n",
    "\n",
    "    def similarPairs(self, words: List[str]) -> int:\n",
    "        n = 0\n",
    "        for i in range(len(words)):\n",
    "            for j in range(i+1, len(words)):\n",
    "                if self.isSame(words[i], words[j]):\n",
    "                    n += 1\n",
    "        return n\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def similarPairs(self, words: List[str]) -> int:\n",
    "        counter=defaultdict(int)\n",
    "        for word in words:\n",
    "            t = 0\n",
    "            for c in word:\n",
    "                t|=1<<(ord(c)-ord('a'))\n",
    "            counter[t]+=1\n",
    "        ans = 0\n",
    "        for v in counter.values():\n",
    "            ans+=(v-1)*v//2\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def similarPairs(self, words: List[str]) -> int:\n",
    "        char_set_count = collections.defaultdict(int)\n",
    "\n",
    "        for word in words:\n",
    "            char_set = set()\n",
    "            for c in word:\n",
    "                char_set.add(c)\n",
    "            char_set_count[tuple(sorted(char_set))] += 1\n",
    "        return reduce(lambda x, y : x + y, [char_set_count[key] * (char_set_count[key] - 1) // 2 for key in char_set_count])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def similarPairs(self, words: List[str]) -> int:\n",
    "        return sum(\n",
    "            set(words[i])==set(words[j])\n",
    "            for i in range(len(words))\n",
    "            for j in range(i+1, len(words))\n",
    "        )\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def similarPairs(self, words: List[str]) -> int:\n",
    "        seen = Counter()\n",
    "        res = 0\n",
    "        for w in words:\n",
    "            w = ''.join(sorted(set(w)))\n",
    "            res += seen[w]\n",
    "            seen[w] += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def similarPairs(self, words: List[str]) -> int:\n",
    "        hash_d = {}\n",
    "        res = 0\n",
    "        for word in words:\n",
    "            word_set = \"\".join(list(set(word)))\n",
    "            res += hash_d.get(word_set,0)\n",
    "            hash_d[word_set] = hash_d.get(word_set,0) + 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def similarPairs(self, words: List[str]) -> int:\n",
    "        ans,cnt = 0,Counter()\n",
    "        for s in words:\n",
    "            mask = 0\n",
    "            for c in s:\n",
    "                mask |= 1 << (ord(c) - ord('a'))\n",
    "            ans += cnt[mask]\n",
    "            cnt[mask] += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def similarPairs(self, words: List[str]) -> int:\n",
    "        res = 0\n",
    "        for i in range(len(words)):\n",
    "            for j in range(i+1,len(words)):\n",
    "                if set(words[i]) == set(words[j]):\n",
    "                    res += 1\n",
    "        \n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def similarPairs(self, words: List[str]) -> int:\n",
    "        return sum(\n",
    "            set(words[i]) == set(words[j]) \n",
    "            for i in range(len(words)) \n",
    "            for j in range(i + 1, len(words))\n",
    "        )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def similarPairs(self, words: List[str]) -> int:\n",
    "        ans, cnt = 0, Counter()\n",
    "        for s in words:\n",
    "            mask = 0\n",
    "            for c in s:\n",
    "                mask |= 1 << (ord(c) - ord('a'))\n",
    "            ans += cnt[mask]\n",
    "            cnt[mask] += 1\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def similarPairs(self, words: List[str]) -> int:\n",
    "        ret = 0\n",
    "        for index1 in range(len(words)):\n",
    "            for index2 in range(index1+1, len(words)):\n",
    "                if set(words[index1]) == set(words[index2]):\n",
    "                    ret += 1\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def similarPairs(self, words: List[str]) -> int:\n",
    "        def cal(st):\n",
    "            n=0\n",
    "            for c in st:\n",
    "                idx=ord(c)-ord('a')\n",
    "                n|=(1<<idx)\n",
    "            return n\n",
    "        \n",
    "        s=0\n",
    "        l=len(words)\n",
    "        for i in range(l):\n",
    "            for j in range(i+1,l):\n",
    "                if cal(words[i]) == cal(words[j]):\n",
    "                    s+=1\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def similarPairs(self, words: List[str]) -> int:\n",
    "        ordA = ord('a')\n",
    "        maskCounter = defaultdict(int)\n",
    "        for word in words:\n",
    "            mask = reduce(lambda a, b: a | (1<<(ord(b)-ordA)), word, 0)\n",
    "            maskCounter[mask] += 1\n",
    "        return sum(cnt*(cnt-1)//2 for cnt in maskCounter.values() if cnt > 1)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def similarPairs(self, words: List[str]) -> int:\n",
    "        b=0\n",
    "        for i in range(len(words)):\n",
    "            for j in range(i+1,len(words)):\n",
    "               if set(words[i])==set(words[j]):\n",
    "                   b+=1\n",
    "\n",
    "        return b\n",
    "                       \n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def similarPairs(self, words: List[str]) -> int:\n",
    "        #初始化答案为0\n",
    "        ans=0\n",
    "        from collections import Counter\n",
    "\n",
    "        for i in range(0,len(words)):\n",
    "            for w in range(i+1,len(words)):\n",
    "                if Counter(words[i]).keys()==Counter(words[w]).keys():\n",
    "                    ans+=1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def similarPairs(self, words: List[str]) -> int:\n",
    "        n = len(words)\n",
    "        ans = 0\n",
    "        # compare = [] * n\n",
    "        for i in range(n):\n",
    "            a = list(set(words[i]))\n",
    "            a.sort()\n",
    "            for j in range(i + 1, n):\n",
    "                if i != j:\n",
    "                    b = list(set(words[j]))\n",
    "                    b.sort()\n",
    "                    if a == b:\n",
    "                        ans += 1\n",
    "        return ans\n",
    "          \n",
    "        \n",
    "        # # 每个字符串用一个整数表示（每个小数排列起来用0/1表示，1存在，0不存在）\n",
    "        # ans, cnt = 0, Counter()   # cnt 用于记录每个整数的出现次数\n",
    "        \n",
    "        # for s in words:\n",
    "        #     mask = 0\n",
    "        #     # 将s转换成整数\n",
    "        #     for c in s:\n",
    "        #         # ord(c) - ord(\"a\") 表示当前字母的askii码减去a的askii码\n",
    "        #         mask |= 1 << (ord(c) - ord('a'))  # 将此值加到二进制数中\n",
    "        #     ans += cnt[mask]\n",
    "        #     cnt[mask] += 1   # 选出字符串不允许相等，必须先统计答案\n",
    "        # return ans\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def similarPairs(self, words: List[str]) -> int:\n",
    "        ans, cnt = 0,Counter()\n",
    "        for s in words:\n",
    "            mask = 0\n",
    "            for c in s:\n",
    "                mask |= 1 << ord(c) - ord('a')\n",
    "            ans += cnt[mask]\n",
    "            cnt[mask] += 1\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def similarPairs(self, words: List[str]) -> int:\n",
    "        return sum(set(words[i])==set(words[j]) for i in range(len(words)) for j in range(i+1,len(words)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def similarPairs(self, words: List[str]) -> int:\n",
    "        answer = 0\n",
    "        for i in range(len(words)):\n",
    "            a = list(set(words[i]))\n",
    "            a.sort()\n",
    "            for j in range(i+1, len(words)):\n",
    "                if i != j:\n",
    "                    b = list(set(words[j]))\n",
    "                    b.sort()\n",
    "                    if a == b:\n",
    "                        answer += 1\n",
    "        return answer\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def similarPairs(self, words: List[str]) -> int:\n",
    "        ans, cnt = 0, Counter()\n",
    "        for s in words:\n",
    "            mask = 0\n",
    "            for c in s:\n",
    "                mask |= 1 << (ord(c) - ord('a'))\n",
    "            ans += cnt[mask]\n",
    "            cnt[mask] += 1\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def similarPairs(self, words: List[str]) -> int:\n",
    "        def getSet(s):\n",
    "            ret = set()\n",
    "            for i in range(len(s)):\n",
    "                ret.add(s[i])\n",
    "            return ret\n",
    "        ans = 0\n",
    "        for i in range(len(words)):\n",
    "            s1 = getSet(words[i])\n",
    "            for j in range(i + 1, len(words)):\n",
    "                s2 = getSet(words[j])\n",
    "                andset = len(s1 & s2)\n",
    "                if len(s1) == andset and len(s2) == andset:\n",
    "                    ans += 1\n",
    "        return ans\n",
    "                \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def similarPairs(self, words: List[str]) -> int:\n",
    "        n = len(words)\n",
    "        words_list = [list(set(word)) for word in words]\n",
    "        new_list = []\n",
    "        for wl in words_list:\n",
    "            wl.sort()\n",
    "            new_list.append(wl)\n",
    "        count = 0\n",
    "        for i in range(n-1):\n",
    "            for j_list in new_list[i+1:]:\n",
    "                if new_list[i] == j_list:\n",
    "                    count += 1\n",
    "        return count\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def similarPairs(self, words: List[str]) -> int:\n",
    "        n = len(words)\n",
    "        ans = 0\n",
    "        for i in range(n):\n",
    "            ci = sorted(list(set(words[i])))\n",
    "            # print(ci)\n",
    "            for j in range(i + 1, n):\n",
    "                cj = sorted(list(set(words[j])))\n",
    "                if ci == cj:\n",
    "                    ans += 1\n",
    "        return ans\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def similarPairs(self, words: List[str]) -> int:\n",
    "        return sum(v * (v-1) >> 1 for v in Counter(map(frozenset, words)).values())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def similarPairs(self, words: List[str]) -> int:\n",
    "        a = 0\n",
    "        for i in range(len(words)-1):\n",
    "            for n in range(i+1,len(words)):\n",
    "                if set(words[i])==set(words[n]):\n",
    "                    a += 1\n",
    "        return a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def similarPairs(self, words: List[str]) -> int:\n",
    "        ans, cnt = 0, Counter()\n",
    "        for s in words:\n",
    "            mask = 0\n",
    "            for c in s:\n",
    "                mask |= 1 << (ord(c) - ord('a'))\n",
    "            ans += cnt[mask]\n",
    "            cnt[mask] += 1\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def similarPairs(self, words: List[str]) -> int:\n",
    "        a = 0\n",
    "        for i in range(len(words)-1):\n",
    "            for n in range(i+1,len(words)):\n",
    "                if set(words[i])==set(words[n]):\n",
    "                    a += 1\n",
    "        return a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def similarPairs(self, words: List[str]) -> int:\n",
    "        import collections\n",
    "        count = len(words)\n",
    "        result = 0\n",
    "        for i in range(count-1):\n",
    "            for j in range(1+i, count):\n",
    "                i_dic = set(collections.Counter(words[i]))\n",
    "                j_dic = set(collections.Counter(words[j]))\n",
    "                if i_dic == j_dic:\n",
    "                    result += 1\n",
    "        return result\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def similarPairs(self, words: list) -> int:\n",
    "        hash_d = {}\n",
    "        res = 0\n",
    "        for word in words:\n",
    "            word_set = \"\".join(sorted(list(set(word))))\n",
    "            res += hash_d.get(word_set,0)\n",
    "            hash_d[word_set] = hash_d.get(word_set,0) + 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def similarPairs(self, words: List[str]) -> int:\n",
    "        ans=0\n",
    "        for i in range(len(words)):\n",
    "            set1=set(words[i])\n",
    "            for j in range(i+1,len(words)):\n",
    "                set2=set(words[j])\n",
    "                if set1==set2:\n",
    "                    ans=ans+1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def get_unichar(self, string):\n",
    "        res = set()\n",
    "        for i in string:\n",
    "            res.add(i)\n",
    "        res = list(res)\n",
    "        res.sort()\n",
    "        return ''.join(res)\n",
    "\n",
    "    def similarPairs(self, words: List[str]) -> int:\n",
    "        char_dic = {}\n",
    "        for e in words:\n",
    "            this_unc = self.get_unichar(e)\n",
    "            if this_unc in char_dic:\n",
    "                char_dic[this_unc]+=1\n",
    "            else:\n",
    "                char_dic[this_unc]=1\n",
    "        \n",
    "        cnt = 0\n",
    "        for key in char_dic:\n",
    "            cnt += int(char_dic[key]*(char_dic[key]-1)/2)\n",
    "\n",
    "        return cnt\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def similarPairs(self, words: List[str]) -> int:\n",
    "        # seen = Counter()\n",
    "        # res = 0\n",
    "        # for w in words:\n",
    "        #     w = ''.join(sorted(set(w)))\n",
    "        #     res += seen[w]\n",
    "        #     seen[w] += 1\n",
    "        # return res\n",
    "\n",
    "        # -------------------------------------\n",
    "        seen = Counter()\n",
    "        res = 0\n",
    "        for w in words:\n",
    "            mask = 0\n",
    "            for ch in w:\n",
    "                mask |= 1 << (ord(ch)-ord('a'))\n",
    "            res += seen[mask]\n",
    "            seen[mask] += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def similarPairs(self, words: List[str]) -> int:\n",
    "        s=len(words)\n",
    "        c=0\n",
    "        for i in range(0,s):\n",
    "            for j in range(i+1,s):\n",
    "                a=set(words[i])\n",
    "                b=set(words[j])\n",
    "                if a==b:\n",
    "                    c+=1\n",
    "        return c\n",
    "                \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def similarPairs(self, words: List[str]) -> int:\n",
    "        def wordcompare(word1,word2):\n",
    "            w1,w2 = [],[]\n",
    "            for w in word1:\n",
    "                if w not in w1:w1.append(w)\n",
    "            for w in word2:\n",
    "                if w not in w2:w2.append(w)\n",
    "            return sorted(w1)==sorted(w2)\n",
    "\n",
    "        ans = 0\n",
    "        for i in range(len(words)):\n",
    "            for j in range(i+1,len(words)):\n",
    "                if wordcompare(words[i],words[j]):ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def similarPairs(self, words: list) -> int:\n",
    "        hash_d = {}\n",
    "        res = 0\n",
    "        for word in words:\n",
    "            word_set = \"\".join(sorted(list(set(word))))\n",
    "            res += hash_d.get(word_set,0)\n",
    "            hash_d[word_set] = hash_d.get(word_set,0) + 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def similarPairs(self, words: List[str]) -> int:\n",
    "        lst = [\"\".join(sorted(list(set(w)))) for w in words]\n",
    "        dic = Counter(lst)\n",
    "        ans = 0\n",
    "        for v in dic.values():\n",
    "            ans += v * (v - 1) // 2\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def similarPairs(self, words: List[str]) -> int:\n",
    "        count = 0\n",
    "        for i in range(len(words)-1):\n",
    "            for j in range(i+1, len(words)):\n",
    "                if set(words[i]) == set(words[j]):\n",
    "                    count += 1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def similarPairs(self, words: List[str]) -> int:\n",
    "        ans, cnt = 0, Counter()\n",
    "        for s in words:\n",
    "            mask = 0\n",
    "            for c in s:\n",
    "                mask |= 1 << (ord(c) - ord('a'))\n",
    "            ans += cnt[mask]\n",
    "            cnt[mask] += 1\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def similarPairs(self, words: List[str]) -> int:\n",
    "        a=0\n",
    "        n=len(words)\n",
    "        for i in range(n-1):\n",
    "            for j in range(i+1,n):\n",
    "                if set(words[i])==set(words[j]):\n",
    "                    a+=1\n",
    "        return a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def similarPairs(self, words: List[str]) -> int:\n",
    "        wd = []\n",
    "        for word in words:\n",
    "            wd.append(Counter(word))\n",
    "        n = len(words)\n",
    "        cnt = 0\n",
    "        if n == 1:\n",
    "            return cnt\n",
    "        for i in range(n - 1):\n",
    "            for j in range(i + 1, n):\n",
    "                if wd[i].keys() == wd[j].keys():\n",
    "                    cnt += 1\n",
    "        return cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def similarPairs(self, words: List[str]) -> int:\n",
    "        # 初始化答案为0\n",
    "        ans = 0\n",
    "        from collections import Counter\n",
    "\n",
    "        # 暴力判断 与哈希表的键是否相等\n",
    "        for i in range(0,len(words)):\n",
    "            for w in range(i+1,len(words)):\n",
    "                if Counter(words[i]).keys() == Counter(words[w]).keys():\n",
    "                    # 相等就ans+1\n",
    "                    ans+=1\n",
    "        return ans\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def similarPairs(self, words: List[str]) -> int:\n",
    "        ans, cnt = 0, Counter()\n",
    "        for s in words:\n",
    "            mask = 0\n",
    "            for c in s:\n",
    "                mask |= 1 << (ord(c) - ord('a'))\n",
    "            ans += cnt[mask]\n",
    "            cnt[mask] += 1\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def similarPairs(self, words: List[str]) -> int:\n",
    "        s=0\n",
    "        for i in range(len(words)-1):\n",
    "            for j in range(i+1,len(words)):\n",
    "                if sorted(set(words[i]))==sorted(set(words[j])):\n",
    "                    s+=1\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def similarPairs(self, words: List[str]) -> int:\n",
    "        ans, cnt = 0, Counter()\n",
    "        for s in words:\n",
    "            mask = 0\n",
    "            for c in s:\n",
    "                mask |= 1 << (ord(c) - ord('a'))\n",
    "            ans += cnt[mask]\n",
    "            cnt[mask] += 1\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def similarPairs(self,words : list[str])->int:\n",
    "        n = len(words)\n",
    "        k = 0\n",
    "        for i in range(n):\n",
    "            for j in range(i+1,n):\n",
    "                if set(words[i])==set(words[j]):\n",
    "                    k += 1\n",
    "        return k"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def similarPairs(self, words: List[str]) -> int:\n",
    "        arr = []\n",
    "        for word in words:\n",
    "            tmp = list(set(list(word)))\n",
    "            tmp.sort()\n",
    "            key = \"\".join(tmp)\n",
    "            arr.append(key)\n",
    "        ans = 0\n",
    "        for i in range(len(arr)):\n",
    "            for j in range(i+1, len(arr)):\n",
    "                if arr[i] == arr[j]:\n",
    "                    ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def similarPairs(self, words: List[str]) -> int:\n",
    "        return sum(v * (v - 1) // 2 for v in Counter(frozenset(w) for w in words).values())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def similarPairs(self, words: List[str]) -> int:\n",
    "        a = 0\n",
    "        for i in range(len(words)-1):\n",
    "            for n in range(i+1,len(words)):\n",
    "                if set(words[i])==set(words[n]):\n",
    "                    a += 1\n",
    "        return a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def similarPairs(self, words: list[str]) -> int:\n",
    "        res=0\n",
    "        for i in range(len(words)):\n",
    "            for j in range(len(words)):\n",
    "                if i!=j:\n",
    "                    if set(words[i])^set(words[j])==set():\n",
    "                        res+=1\n",
    "        return int(res/2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def similarPairs(self, words: List[str]) -> int:\n",
    "        n=len(words)\n",
    "        ans=0\n",
    "        for i in range(n):\n",
    "            for j in range(i+1,n):\n",
    "                if Counter(words[i]).keys()==Counter(words[j]).keys():\n",
    "                    ans+=1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def similarPairs(self, words: List[str]) -> int:\n",
    "        result=0\n",
    "        for i in range(len(words)):\n",
    "            for j in range(i+1,len(words)):\n",
    "                a1=set(words[i])\n",
    "                a2=set(words[j])\n",
    "                if(a1==a2):\n",
    "                    result+=1\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def similarPairs(self, words: List[str]) -> int:\n",
    "        ws = [''.join(set(sorted(w))) for w in words]\n",
    "        ds = {}\n",
    "        for w in ws:\n",
    "            ds[w] = ds.get(w,0)\n",
    "            ds[w] += 1\n",
    "        ans = 0\n",
    "        for v in ds.values():\n",
    "            ans += v * (v-1) // 2\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def similarPairs(self, words: List[str]) -> int:\n",
    "        return sum(set(i) == set(j) for i, j in combinations(words, 2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def similarPairs(self, words: List[str]) -> int:\n",
    "        lst = Counter([\"\".join(sorted(list(set(w)))) for w in words])\n",
    "        return sum([v * (v - 1) // 2 for v in lst.values()])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def similarPairs(self, words: List[str]) -> int:\n",
    "        w = Counter(map(frozenset, words))   \n",
    "        result = 0\n",
    "        for x in w:\n",
    "            if w[x] >= 2:\n",
    "                result += math.comb(w[x], 2)\n",
    "        return result\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def similarPairs(self, words: List[str]) -> int:\n",
    "        setlist = []\n",
    "        for data in words:\n",
    "            tmp = set(list(data))\n",
    "            setlist.append(tmp)\n",
    "        cnt = 0\n",
    "        for i,d in enumerate(setlist):\n",
    "            for j in range(i+1,len(setlist)):\n",
    "                if setlist[i] == setlist[j]:\n",
    "                    cnt+=1\n",
    "        return cnt \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def similarPairs(self, words: List[str]) -> int:\n",
    "        ans, cnt = 0, Counter()\n",
    "        for s in words:\n",
    "            mask = 0\n",
    "            for c in s:\n",
    "                mask |= 1 << (ord(c) - ord('a'))\n",
    "            ans += cnt[mask]\n",
    "            cnt[mask] += 1\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def similarPairs(self, words: List[str]) -> int:\n",
    "        dictn = []\n",
    "        for w in words:\n",
    "            dictn.append(Counter(w))\n",
    "        res = 0\n",
    "        for i in range(len(dictn)):\n",
    "            for j in range(i+1,len(dictn)):\n",
    "                if dictn[i].keys() == dictn[j].keys():\n",
    "                    res += 1\n",
    "        return res\n",
    "\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def similarPairs(self, words: List[str]) -> int:\n",
    "      ret = 0\n",
    "      tmp = Counter(map(frozenset, words))\n",
    "      for t in tmp.values():\n",
    "        ret += t * (t-1) >> 1\n",
    "      return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def similarPairs(self, words: List[str]) -> int:\n",
    "        res = 0\n",
    "        dic = {}\n",
    "        for item in words:\n",
    "            tmp = []\n",
    "            for i in range(len(item)):\n",
    "                if item[i] not in tmp:\n",
    "                    tmp.append(item[i])\n",
    "            tmp.sort()\n",
    "            s_tmp = ''.join(tmp)\n",
    "            dic[s_tmp] = dic.get(s_tmp,0)+1\n",
    "        for num in list(dic.values()):\n",
    "            if num>=2:\n",
    "                res += num*(num-1)/2\n",
    "        return int(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def similarPairs(self, words: List[str]) -> int:\n",
    "        count=0\n",
    "        n=len(words)\n",
    "        for i in range(n-1):\n",
    "            for j in range(i+1,n):\n",
    "                if set(words[i])==set(words[j]):\n",
    "                    count+=1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import defaultdict\n",
    "class Solution:\n",
    "    def similarPairs(self, words: List[str]) -> int:\n",
    "        vis=defaultdict(int)\n",
    "        ans=0\n",
    "        for w in words:\n",
    "            tmp=set()\n",
    "            for ch in w:\n",
    "                tmp.add(ch)\n",
    "            k=tuple(sorted(list(tmp)))\n",
    "            if k in vis:\n",
    "                ans+=vis[k]\n",
    "            vis[k]+=1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def similarPairs(self, words: List[str]) -> int:\r\n",
    "        condition = dict()\r\n",
    "        for i in words:\r\n",
    "            if tuple(sorted(list(set(list(i))))) not in condition.keys():\r\n",
    "                condition[tuple(sorted(list(set(list(i)))))] = 1\r\n",
    "            else:\r\n",
    "                condition[tuple(sorted(list(set(list(i)))))] += 1\r\n",
    "        ans = 0\r\n",
    "        for i in condition.values():\r\n",
    "            ans += int(i*(i-1)/2)\r\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def similarPairs(self, words: List[str]) -> int:\n",
    "        for i in range(len(words)):\n",
    "            words[i] = set(words[i])\n",
    "        res = 0\n",
    "        for i in range(len(words)-1):\n",
    "            for j in range(i+1, len(words)):\n",
    "                if words[i] == words[j]:\n",
    "                    res += 1\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def similarPairs(self, words: List[str]) -> int:\n",
    "        n = len(words)\n",
    "        words = [set(word) for word in words]\n",
    "        res = 0\n",
    "        for i in range(n):\n",
    "            for j in range(i+1, n):\n",
    "                if words[i] == words[j]:\n",
    "                    res += 1\n",
    "        \n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def similarPairs(self, words: List[str]) -> int:\n",
    "        dictn = []\n",
    "        for w in words:\n",
    "            dictn.append(set(w))\n",
    "        res = 0\n",
    "        for i in range(len(dictn)):\n",
    "            for j in range(i+1,len(dictn)):\n",
    "                if dictn[i] == dictn[j]:\n",
    "                    res += 1\n",
    "        return res\n",
    "\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def similarPairs(self, words: list[str]) -> int:\r\n",
    "        n = 0\r\n",
    "        for i in range(len(words)-1):\r\n",
    "            for j in range(i+1,len(words)):\r\n",
    "                flag = 1\r\n",
    "                for a in words[i]:\r\n",
    "                    if(a not in words[j]):\r\n",
    "                        flag = 0\r\n",
    "                        break\r\n",
    "                if(flag):\r\n",
    "                    for a in words[j]:\r\n",
    "                        if(a not in words[i]):\r\n",
    "                            flag = 0\r\n",
    "                            break\r\n",
    "                if(flag):\r\n",
    "                    print(words[i],words[j])\r\n",
    "                    n += 1\r\n",
    "        return n\r\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def similarPairs(self, words: List[str]) -> int:\n",
    "        word = []\n",
    "        num = 0\n",
    "        for i in words:\n",
    "            m = set(list(i))\n",
    "            num += word.count(m)\n",
    "            word.append(m)\n",
    "        return num\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def similarPairs(self, words: List[str]) -> int:\n",
    "        return sum([v * (v - 1) // 2 for v in Counter([\"\".join(sorted(list(set(w)))) for w in words]).values()])"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
